import userDao, { UserDao } from "../dao/userDao";
import { Application } from "pinus";
import { OnlineStatus } from "../consts/code";

export default function (app: Application) {
    return new UserService(app)
}

export class UserService {
    private _userDao: UserDao

    public constructor(private app: Application) {
        this._userDao = userDao(app)
    }
    /**
     * 根据Id获取玩家信息
     * @param id
     * @returns
     */
    public async getUserInfoById(id: number) {
        let user = await this._userDao.getUserInfoById(id)
        let userResults = user.results
        if (userResults.length !== 1) {
            return null
        }
        return {
            userId: userResults[0].id,
            username: userResults[0].username,
            coin: userResults[0].coin,
            status: userResults[0].status,
        }
    }

    /**
     * 根据channelId获取玩家信息
     * @param channelId
     * @returns
     */
    public async haveUserBychannelId(channelId: string) {
        let user = await this._userDao.getUserInfoBychannel_id(channelId)
        let userResults = user.results
        if (userResults.length !== 1) {
            return -1
        }
        return userResults[0].id
    }

    /**
     * 获取游戏端地址
     * @param channelId
     * @returns
     */
    public async getGameClientUrl() {
        let user = await this._userDao.getGameClientUrl()
        let userResults = user.results
        return userResults[0].value
    }

    /**
     * 根据channel_id 创建玩家  返回uid
     * @param id
     * @returns
     */
    public async createUserReturnId(values: any) {
        await this._userDao.createUserReturnId(values)
        //获取玩家id
        let user = await this._userDao.getUserInfoBychannelId(values.channel_id)
        let userResults = user.results
        if (userResults.length !== 1) {
            return -1
        }
        return userResults[0].id
    }

    

    /**
     * 根据Id获取没在游戏中的玩家信息
     * @param id
     * @returns
     */
    public async getHallUserInfoById(id: number) {
        let user = await this._userDao.getHallUserInfoById(id)
        let userResults = user.results
        if (userResults.length !== 1) {
            return null
        }
        return {
            coin: userResults[0].coin
        }
    }



    /**
     * 获取玩家金币
     * @param userId
     * @returns
     */
    public async getUserCoinById(userId: number): Promise<number> {
        let coin = await this._userDao.getUserCoinById(userId)
        let coinResults = coin.results
        if (coinResults.length !== 1) {
            return null
        }
        return coinResults[0].coin
    }
    /**
     * 加载玩家数据，并且锁住
     * @param userId
     * @returns
     */
    public async loadUserInfo(userId: number) {
        let user = await this._userDao.loadUserInfoById(userId)
        let userResults = user.results
        return {
            userId: userResults[0].id,
            username: userResults[0].username,
            coin: userResults[0].coin,
            channelId: parseInt(userResults[0].channelId),
            // currency: userResults[0].currency,
            // currencyNum: userResults[0].currencyNum,
            expand: userResults[0].expand
        }
    }
    /**
     * 玩家数据落地
     * @param userId
     * @param coin
     */
    public async unlockCoinById(userId: number) {
        let { results } = await this._userDao.unlockCoinById(userId)
        return results.affectedRows === 1
    }

    /**
     * 更新玩家金币
     * @param userId
     * @param coin
     * @returns
     */
    public async updateUserCoin(userId: number, coin: number) {
        let { results } = await this._userDao.updateCoinById(userId, coin)
        return results.affectedRows === 1
    }


    /**
     * 修改玩家在线状态
     * @param userId
     * @returns
     */
    public async updateUserOnline(userId: number, online: OnlineStatus) {
        let { results } = await this._userDao.updateUserOnlineById(userId, online)
        return results.affectedRows === 1
    }


    /**
     * 批量修改玩家金币
     */
    public async batchUpdateUserCoin() {

    }

    /**
     * 用户上分更新金币
     * @param userId
     * @param userCoin
     * @param addCoin
     */
    async updateCoin(userId: number, userCoin: number, coin: number) {
        let { results } = await this._userDao.updateCoin(userId, userCoin, coin)
        return results.affectedRows === 1;
    }
    /**
     * 用户下分更新金币
     * @param userId
     * @param userCoin
     * @param addCoin
     */
    async decreaseCoin(userId: number, userCoin: number, coin: number) {
        let { results } = await this._userDao.decreaseCoin(userId, userCoin, coin)
        return results.affectedRows === 1;
    }

    async getChannelInfos(): Promise<{ channelId: number; notifyUrl: string; notifyBalanceUrl: string }[]> {
        let channelInfos = []
        let { results } = await this._userDao.getChannelInfos()
        results.forEach(e => {
            channelInfos.push(e)
        })
        return results
    }

    async updateCoinAndUnlock(userId: number, coin: number) {
        let { results } = await this._userDao.updateCoinAndUnlock(userId, coin)
        return results.affectedRows === 1
    }
}
